<?php

namespace App\Admin\Controllers\Admin;


use App\Admin\Actions\Admin\AgentQywx;
use App\Admin\Actions\Admin\AgentRecharge;
use App\Models\AdminUser;
use App\Models\AgentInfo;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use function __;

class AgentInfoController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '代理列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new AgentInfo());

        $grid->column('id', __('Id'));
        $grid->column('adminUser.name', __('代理名称'));
        $grid->column('balance', __('代理账户余额'));
        $grid->column('qywx', __('企业微信'));
        $grid->column('created_at', __('创建时间'));

        $grid->actions(function ($action) use ($grid) {
            $action->add(new AgentRecharge());
            $action->add(new AgentQywx());
            $action->disableView();
            $action->disableEdit();
            $action->disableDelete();
        });

        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new AdminUser());

        $form->text('username', __('账号'));
        $form->password('password', __('密码'));
        $form->text('name', __('昵称'));
        $form->image('avatar', __('头像'));


        $form->saving(function (Form $form) {
            if ($form->password && $form->model()->password != $form->password) {
                $form->password = Hash::make($form->password);
            }
        });

        return $form;
    }
}
